package com.sfh.cpc.mass;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.library.StatusBarUtil;
import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.entity.ENote;
import com.sfh.cpc.com.entity.EUser;
import com.sfh.cpc.com.entity.RspPage;
import com.sfh.cpc.com.entity.RxEventVip;
import com.sfh.cpc.com.refresh.SwipeRefreshMoreLayout;
import com.sfh.cpc.com.server.IUserServer;
import com.sfh.cpc.com.ui.VipLimit;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.mass.adapter.MassNoteAdapter;
import com.sfh.cpc.mass.model.MassNoteModel;
import com.sfh.cpc.note.NoteActivity;
import com.sfh.cpc.note.NoteAddActivity;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.event.RxBusEvent;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleActivity;
import com.sfh.lib.ui.dialog.DialogBuilder;

public class MassActivity extends AbstractLifecycleActivity<MassNoteModel> implements SwipeRefreshMoreLayout.OnLoadingListener, BaseQuickAdapter.OnItemClickListener {

    public static void startUI(Context context, EMass mass) {

        Intent intent = new Intent(context, MassActivity.class);
        intent.putExtra("DATA", mass);
        context.startActivity(intent);
    }

    TextView tvTitle;
    WidgetBaseToolbar toolbar;
    SwipeRefreshMoreLayout layout;
    ImageView ivAdd;
    TextView tvNew;
    TextView tvHot;

    MassNoteAdapter adapter;
    int nowPage;
    EMass mMass;

    int type = 0;
    View lyContent;
    MassView noteView;
    int height = 400;
    int overallXScroll = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_activity);
        this.toolbar = findViewById(R.id.toolbar);
        StatusBarUtil.setTranslucentForImageView(this, 0, this.toolbar);
        this.tvTitle = this.toolbar.setTitle("");
        this.tvTitle.setTextColor(Color.WHITE);
        this.ivAdd = findViewById(R.id.ivAdd);
        this.layout = findViewById(R.id.swipeRefreshMoreLayout);
        this.lyContent = findViewById(R.id.lyContent);
        this.tvNew = findViewById(R.id.tvNew);
        this.tvHot = findViewById(R.id.tvHot);
        this.tvNew.setOnClickListener(onClickListener);
        this.tvHot.setOnClickListener(onClickListener);

        this.adapter = new MassNoteAdapter();
        this.noteView = new MassView(this);

        this.noteView.selectLisnter(onClickListener);
        this.adapter.addHeaderView(noteView);

        this.layout.setAdapter(adapter, true);
        this.layout.addItemDecoration(R.dimen.px_7);
        this.layout.setOnItemClickListener(this);
        this.layout.setOnLoadingListener(this);

        this.layout.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                //dy > 0 时为手指向上滚动,列表滚动显示下面的内容
                //dy < 0 时为手指向下滚动,列表滚动显示上面的内容
                // 累加y值 解决滑动一半y值为0
                overallXScroll = overallXScroll + dy;
                if (overallXScroll <= 0) {
                    //设置标题的背景颜色
                    setalpha(0);

                } else if (overallXScroll > 0 && overallXScroll <= height) {
                    //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) overallXScroll / height;
                    int alpha = (int) (255 * scale);
                    setalpha(alpha);
                } else {
                    setalpha(255);
                }

                if (overallXScroll >= 500) {
                    lyContent.setVisibility(View.VISIBLE);
                } else if (overallXScroll <= 400) {
                    lyContent.setVisibility(View.GONE);
                }

            }
        });

        mMass = getIntent().getParcelableExtra("DATA");
        if (mMass == null) {
            return;
        }

        this.tvTitle.setText(mMass.getMassTitle());


        if (VipLimit.lookMass(this, mMass)) {
            this.getViewModel().vipLookMass(mMass.getMassUserId());
            this.noteView.showData(mMass);
            this.layout.setVisibility(View.VISIBLE);
            this.layout.onAutoRefresh();
        } else {
            this.layout.setVisibility(View.GONE);
        }

    }

    public void setalpha(int alpha) {

        int argb = Color.argb(alpha, 255, 255, 255);
        this.toolbar.setBackgroundColor(argb);
        this.tvTitle.setTextColor(Color.argb(alpha, 0, 0, 0));
        StatusBarUtil.setTranslucentForImageView(this, alpha, this.toolbar);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == tvHot || noteView.getTvHot() == v) {
                tvHot.setTextColor(ContextCompat.getColor(MassActivity.this, R.color.bg));
                noteView.getTvHot().setTextColor(ContextCompat.getColor(MassActivity.this, R.color.bg));

                tvNew.setTextColor(ContextCompat.getColor(MassActivity.this, R.color.color_999999));
                noteView.getTvNew().setTextColor(ContextCompat.getColor(MassActivity.this, R.color.color_999999));
                type = 1;
            } else {
                tvNew.setTextColor(ContextCompat.getColor(MassActivity.this, R.color.bg));
                noteView.getTvNew().setTextColor(ContextCompat.getColor(MassActivity.this, R.color.bg));

                tvHot.setTextColor(ContextCompat.getColor(MassActivity.this, R.color.color_999999));
                noteView.getTvHot().setTextColor(ContextCompat.getColor(MassActivity.this, R.color.color_999999));

                type = 0;
            }

            layout.onAutoRefresh();
        }
    };


    @RxBusEvent(from = "登录成功")
    public void onRxEventLogin(EUser user) {
        if (TextUtils.equals("1", mMass.getNoteVip()) && (!user.isVip() || user.getUserId() != mMass.getMassUserId())) {
            //要求VIP
            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("需要VIP限制,激活VIP");
            builder.setHideCancel(true);
            builder.setOKTextListener("激活", new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                    dialog.dismiss();
                    IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                    if (userServer != null) {
                        userServer.openVip(MassActivity.this,mMass.getMassId(),2);
                    }
                }
            });
            showDialog(builder);
        } else {
            this.getViewModel().vipLookMass(mMass.getMassUserId());
            this.mMass.setNoteVip("0");
            this.noteView.showData(mMass);
            this.layout.setVisibility(View.VISIBLE);
            this.layout.onAutoRefresh();
        }
    }

    @RxBusEvent(from = "用户 VIP 激活 or 用户已是VIP ")
    public void onRxEventVIP(RxEventVip vip) {
        this.getViewModel().vipLookMass(mMass.getMassUserId());
        this.mMass.setNoteVip("0");
        this.layout.setVisibility(View.VISIBLE);
        this.layout.onAutoRefresh();
    }

    @LiveDataMatch
    @Override
    public void onRefreshUI() {
        this.nowPage = 1;
        this.getViewModel().pageMassNote(nowPage, mMass.getMassId(), type);
    }

    @Override
    public void onLoadMoreUI() {
        this.nowPage++;
        this.getViewModel().pageMassNote(nowPage, mMass.getMassId(), type);
    }

    @LiveDataMatch
    public void onPageSuccess(RspPage<ENote> data) {

        if (data == null) {
            this.layout.onLoadMoreFail();

        } else {
            this.nowPage = data.getPageNumber();
            this.layout.onRefreshCompale(data.getList(), data.getPageNumber() != 1);
            if (data.getTotalPage() == data.getPageNumber()) {
                this.layout.onLoadAllCompale();
            }
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ENote note = (ENote) adapter.getItem(position);
        NoteActivity.startUI(this, note);
    }

    public void onViewCilcked(View v) {
        final EUser user = AppCacheManager.getCache(BaseAppContent.USER, EUser.class);
        if (user == null || user.getUserId() <= 0) {
            //要求登录
            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("请登录,去登录");
            builder.setHideCancel(true);
            builder.setOKTextListener("登录", new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                    dialog.dismiss();
                    IUserServer userServer = AppCacheManager.getCache(IUserServer.class.getSimpleName(), IUserServer.class);
                    if (userServer != null) {
                        userServer.openLogin(MassActivity.this);
                    }
                }
            });
            showDialog(builder);
            return;
        }
        NoteAddActivity.startUI(MassActivity.this, mMass.getMassId(),TextUtils.equals("1",mMass.getNoteVip()));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK) {
            this.onRefreshUI();
        }
    }
}
